package com.choosefine.devops.dms.system.pages.service;

import com.choosefine.devops.dms.system.pages.Menu;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 邱鹏 on 2017/3/2.
 */
@Service
public class IndexService{
    @Autowired
    ObjectMapper jsonObjectMapper;
    @Autowired
    MongoTemplate mongoTemplate;
    String menuStr = "{\"menuList\":[{\"link\":\"../tpl/main.html\",\"icon\":\"iconfont icon-home1\",\"title\":\"aaaaa\",\"code\":\"1-1\",\"sub\":[{\"link\":\"../tpl/main.html\",\"icon\":\"iconfont icon-home1\",\"title\":\"后台主页\",\"sub\":[]},{\"link\":\"javascript:;\",\"icon\":\"iconfont icon-jiaoseguanli\",\"title\":\"ApiGateway\",\"code\":\"1-1-2\",\"sub\":[{\"link\":\"javascript:;\",\"icon\":\"iconfont icon-geren1\",\"title\":\"配置管理\"},{\"link\":\"javascript:;\",\"icon\":\"iconfont icon-iconfuzhi01\",\"title\":\"监控\"}]},{\"link\":\"javascript:;\",\"icon\":\"iconfont icon-youqinglianjie\",\"title\":\"WAP\",\"sub\":[]}]},{\"link\":\"javascript:;\",\"icon\":\"iconfont icon-jiaoseguanli2\",\"title\":\"bbbb\",\"code\":\"1-2\",\"sub\":[{\"link\":\"javascript:;\",\"icon\":\"iconfont icon-jiaoseguanli2\",\"title\":\"权限管理\"},{\"link\":\"javascript:;\",\"icon\":\"iconfont icon-youqinglianjie\",\"title\":\"Mq管理\",\"sub\":[]},{\"link\":\"javascript:;\",\"icon\":\"iconfont icon-jiaoseguanli4\",\"title\":\"spring cloud 配置中心\",\"sub\":[]},{\"link\":\"javascript:;\",\"icon\":\"iconfont icon-youqinglianjie\",\"title\":\"docker\",\"sub\":[]},{\"link\":\"javascript:;\",\"icon\":\"iconfont icon-jiaoseguanli4\",\"title\":\"sms\",\"sub\":[]},{\"link\":\"javascript:;\",\"icon\":\"iconfont icon-youqinglianjie\",\"title\":\"jpush\",\"sub\":[]},{\"link\":\"javascript:;\",\"icon\":\"iconfont icon-jiaoseguanli4\",\"title\":\"websocket\",\"sub\":[]},{\"link\":\"javascript:;\",\"icon\":\"iconfont icon-youqinglianjie\",\"title\":\"数据同步\",\"sub\":[]},{\"link\":\"javascript:;\",\"icon\":\"iconfont icon-jiaoseguanli4\",\"title\":\"文件管理\",\"sub\":[]}]}]}";

    public Map index() throws IOException {
        Map result = jsonObjectMapper.readValue(menuStr, Map.class);
        List<Menu> topMenuList= mongoTemplate.find(new Query(Criteria.where("parentCode").is("0")), Menu.class);//查询顶部导航一级菜单
        for(Menu topMenu:topMenuList){
        	String code=topMenu.getCode();
        	List<Menu> leftParentMenuList= mongoTemplate.find(new Query(Criteria.where("parentCode").is(code)), Menu.class);//查询左侧导航一级菜单
        	topMenu.getSub().addAll(leftParentMenuList);
        	for(Menu leftMenu:leftParentMenuList){
        		String leftCode=leftMenu.getCode();
        		List<Menu> leftFirstChildMenuList= mongoTemplate.find(new Query(Criteria.where("parentCode").is(leftCode)), Menu.class);//查询左侧导航一级菜单的子菜单
        		if(leftFirstChildMenuList.size()>0){
        			leftMenu.getSub().addAll(leftFirstChildMenuList);
        			for(Menu leftFirstChildMenu: leftFirstChildMenuList){
        				String leftFirstChildCode=leftFirstChildMenu.getCode();
        				List<Menu> secondChildMenuList= mongoTemplate.find(new Query(Criteria.where("parentCode").is(leftFirstChildCode)), Menu.class);
        				if(secondChildMenuList.size()>0){
        					leftFirstChildMenu.getSub().addAll(secondChildMenuList);
        				}
        			}
        		}
        	}
        }
        System.out.println(topMenuList.toString());
        Map<String, Object> map = new HashMap<>();
        map.put("menuList", topMenuList);
        return map;
    }
    /**
     * 插入目录列表（用于新增操作）
     * @param menu
     */
    public void addIndex(Menu menu){
    	System.out.println(menu.toString());
    	mongoTemplate.insert(menu);
    }
}
